package fun.shuofeng.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author 朔风
 * @date 2024-02-29 21:52
 */
public class CompletableFutureCombineDemo {
    public static void main(String[] args) {

        CompletableFuture<Integer> taskA = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "---启动");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 10;
        });
        CompletableFuture<Integer> taskB = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "---启动");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 30;
        });

        CompletableFuture<Integer> completableFuture = taskA.thenCombine(taskB, (x, y) -> {
            System.out.println("---合并任务启动");
            return x + y;
        });

        System.out.println(completableFuture.join());

    }
}


